View Javadoc
1 2 package net.sf.voruta; 3 4 import java.lang.reflect.*; 5 import java.util.*; 6 7 8 /*** 9 * 10 * @author baliuka 11 */ 12 class BeanProperties { 13 14 /*** Creates a new instance of Properties */ 15 private BeanProperties() { 16 17 } 18 19 20 static Property getProperty(Class cls,String name)throws Exception{ 21 22 23 24 StringTokenizer tokenizer = new StringTokenizer(name,"[].->"); 25 LinkedList props = new LinkedList(); 26 Class nextClass = cls; 27 28 LOOP: 29 while(tokenizer.hasMoreTokens()){ 30 31 String token = tokenizer.nextToken(); 32 String lowerCaseToken = token.toLowerCase(); 33 34 if(nextClass == null){ 35 throw new DbParseException(" unknown property type " + token); 36 } 37 Field fields[] = nextClass.getFields(); 38 for(int i = 0; i < fields.length; i++ ){ 39 40 String lowerCaseName = fields[i].getName().toLowerCase(); 41 42 if( lowerCaseToken.equals(lowerCaseName)&& 43 Modifier.isStatic(fields[i].getModifiers())){ 44 45 if(ThreadLocal.class.isAssignableFrom(fields[i].getType())){ 46 ThreadLocal tl = (ThreadLocal)fields[i].get(null); 47 props.add( new ThreadLocalValue(tl) ); 48 nextClass = null; 49 50 continue LOOP; 51 } 52 } 53 } 54 55 Method methods[] = nextClass.getMethods(); 56 57 for(int i = 0; i < methods.length; i++ ){ 58 59 String lowerCaseName = methods[i].getName().toLowerCase(); 60 61 62 if( methods[i].getParameterTypes().length == 0 && 63 ( 64 lowerCaseName.equals("get".concat( lowerCaseToken ) ) || 65 lowerCaseName.equals("is".concat( lowerCaseToken ) ) 66 ) 67 ){ 68 69 70 props.add( new SimpleProperty(token,methods[i]) ); 71 nextClass = methods[i].getReturnType(); 72 73 continue LOOP; 74 75 } 76 77 } 78 79 if (Map.class.isAssignableFrom(nextClass)){ 80 props.add( new MappedProperty(Object.class,token) ); 81 nextClass = null; 82 continue LOOP; 83 84 } 85 86 if(nextClass.isArray()){ 87 int index = Integer.parseInt(token); 88 props.add( new IndexedProperty(nextClass.getComponentType(),token,index) ); 89 nextClass = nextClass.getComponentType(); 90 continue LOOP; 91 } 92 93 if( List.class.isAssignableFrom(nextClass) ){ 94 int index = Integer.parseInt(token); 95 props.add( new IndexedProperty(Object.class,token,index) ); 96 nextClass = null; 97 continue LOOP; 98 } 99 100 throw new DbParseException( "property " + token + " not found in " + nextClass ); 101 } 102 103 if(props.size() == 1 ){ 104 return (Property) props.get(0); 105 }else{ 106 return new NestedProperty( ((Property)props.getLast()).getType() , name, (Property[])props.toArray( new Property[]{} ) ); 107 } 108 109 } 110 111 static abstract class Property{ 112 113 protected String name; 114 protected Class type; 115 116 Property(Class type, String name){ 117 this.name = name; 118 this.type = type; 119 } 120 121 Class getType(){ 122 return type; 123 } 124 abstract Object getValue( Object obj )throws Exception; 125 } 126 127 static class ThreadLocalValue extends Property{ 128 ThreadLocal value; 129 ThreadLocalValue(ThreadLocal value){ 130 super(null,null); 131 this.value = value; 132 } 133 Object getValue( Object obj )throws Exception{ 134 return value.get(); 135 } 136 } 137 138 static class SimpleProperty extends Property{ 139 140 private final Method m; 141 142 SimpleProperty(String name, Method m){ 143 super(m.getReturnType(),name); 144 this.m = m; 145 } 146 147 Object getValue( Object obj )throws Exception{ 148 try{ 149 150 return m.invoke(obj,null); 151 152 }catch(java.lang.reflect.InvocationTargetException ite){ 153 154 if(ite.getTargetException() instanceof Exception){ 155 throw (Exception)ite.getTargetException(); 156 }else{ 157 throw (Error)ite.getTargetException(); 158 } 159 160 } 161 162 } 163 164 } 165 166 static class MappedProperty extends Property{ 167 168 MappedProperty(Class type,String name){ 169 super( type, name ); 170 } 171 172 Object getValue( Object obj )throws Exception{ 173 174 return ((Map)obj).get(name); 175 176 } 177 178 } 179 180 static class IndexedProperty extends Property{ 181 182 private final int index; 183 184 IndexedProperty(Class type, String name, int index){ 185 super(type,name); 186 this.index = index; 187 } 188 189 Object getValue(Object obj) throws Exception { 190 if(obj instanceof List){ 191 return ((List)obj).get(index); 192 }else{ 193 return Array.get(obj,index); 194 } 195 } 196 197 } 198 199 static class NestedProperty extends Property{ 200 201 Property[] props; 202 203 NestedProperty(Class type, String name, Property[] props ){ 204 super(type,name); 205 this.props = props; 206 } 207 208 Object getValue(Object obj) throws Exception { 209 Object value = obj; 210 for(int i = 0, l = props.length; i < l; i++ ){ 211 value = props[i].getValue(value); 212 } 213 return value; 214 } 215 216 } 217 218 219 }

This page was automatically generated by Maven